package com.bbdjt.weixin.web.action.weixin;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.Weixin;
import com.bbdjt.weixin.model.WeixinQueryModel;
import com.bbdjt.weixin.service.WeixinService;
import com.bbdjt.weixin.util.WeiXinUtil;
import com.github.pagehelper.PageInfo;

/**
 * @项目名称: bbdjt
 * @类名称: WeixinAction
 * @类描述: Weixin表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-01-18 04:37:46
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class WeixinAction extends BaseAction {
	
	@Autowired
	private WeixinService weixinService;

	private String page_list = "jsp/weixin/weixinindex";

	private String redirect_list = "redirect:r_weixin";
	
	
	// 分页查询
	@RequestMapping(value = "r_weixin")
	public ModelAndView getListPage(WeixinQueryModel weixin) {
		ModelAndView result = new ModelAndView(page_list);
		weixin.setOrderBy("id asc");
		List<Weixin> weixinList = weixinService.selectByWeixin(weixin);
		result.addObject("pageInfo", new PageInfo<Weixin>(weixinList));
		result.addObject("weixin", weixin);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_weixinXml")
	public ModelAndView getListPageXml(WeixinQueryModel weixin) {
		ModelAndView result = new ModelAndView(page_list);
		weixin.setOrderBy("id asc");
		PageInfo<WeixinQueryModel> pageInfo = weixinService.selectByWeixinQueryModel(weixin);
		List<WeixinQueryModel> weixinList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<WeixinQueryModel>(weixinList));
		result.addObject("weixin", weixin);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_weixinJson", produces = "application/json")
	public ModelAndView pageInfo(WeixinQueryModel weixin) {
		ModelAndView result = new ModelAndView();
		PageInfo<WeixinQueryModel> pageInfo = weixinService.selectByWeixinQueryModel(weixin);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_weixinLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(WeixinQueryModel weixin) {
		weixin.setOrderBy(weixin.getSortname(),weixin.getSortorder());
		PageInfo<WeixinQueryModel> pageInfo = weixinService.selectByWeixinQueryModel(weixin);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_weixin")
	public ModelAndView add(Weixin weixin) {
		ModelAndView result = new ModelAndView();
		if (weixin.getId() != null) {
			weixin = weixinService.selectByKey(weixin.getId());
		}
		result.addObject("weixin", weixin);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_weixin")
	public ModelAndView save(Weixin weixin) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (weixin.getId() != null) {
			weixinService.updateNotNull(weixin);
		} else {
			// 会保存null 值
			weixinService.insertSelective(weixin);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_weixin")
	public String delete(Integer id) {
		weixinService.delete(id);
		return redirect_list;
	}
	
	/**
	 * 获取用户信息并插入数据库后跳转登录页面
	 * @param weixin
	 * @param code
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "add_weixin")
	public ModelAndView add(Weixin weixin,String code,@RequestParam("state") String arr, HttpServletRequest request) throws Exception {
		String[]  strs=arr.split(",");
		Integer id = Integer.valueOf(strs[0]);
		request.getSession().setAttribute("patientId", id);
		System.out.println(request.getSession().getAttribute("patientId"));
		System.out.println(request.getSession().getId());
		ModelAndView result = new ModelAndView("redirect:"+ strs[1] +".action");
		Weixin weixin1 = new Weixin();
		weixin =WeiXinUtil.getWeixinUser(code);
		weixin1 = weixinService.selectByPatient(weixin);
		System.out.println(arr);
		if(weixin1 == null){
			weixin.setPatientId(id);
			weixinService.insertSelective(weixin);
			result.addObject("weixin", weixin);
			return result;
		}else{
			result.addObject("weixin1", weixin1);
			return result;
		}
	}
}